package com.example.lovewordsapplication.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.example.lovewordsapplication.MyApplication;
import com.example.lovewordsapplication.R;
import com.example.lovewordsapplication.adapter.WordsAdapter;
import com.example.lovewordsapplication.entity.UserInfo;
import com.example.lovewordsapplication.entity.Words;
import com.example.lovewordsapplication.ui.fragment.CommunityFragment;
import com.example.lovewordsapplication.ui.fragment.SettingsFragment;
import com.example.lovewordsapplication.ui.fragment.WordsFragment;
import com.example.lovewordsapplication.utils.ToastUtil;
import com.google.android.material.navigation.NavigationView;

import java.util.LinkedList;
import java.util.List;

import cn.bmob.v3.BmobUser;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * @author Leung
 * @date 2020/4/7 23:37
 */
public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener, View.OnClickListener {

    private DrawerLayout dyMenu;
    private Toolbar toolbar;
    private TextView tvUsername;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    /**
     * 初始化View
     */
    private void initView() {
        // 设置侧边打开之后有阴影效果
        dyMenu = (DrawerLayout) findViewById(R.id.dy_menu);
        dyMenu.setDrawerShadow(R.drawable.navigation_drawer_shadow, GravityCompat.START);
        // 设置工具栏
        toolbar = (Toolbar) findViewById(R.id.tb_bar);
        setSupportActionBar(toolbar);
        // 显示工具栏的图标
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeAsUpIndicator(R.drawable.ic_menu);
        }
        // 设置侧边导航的点击事件
        NavigationView nvMenu = (NavigationView) findViewById(R.id.nv_menu);
        nvMenu.setNavigationItemSelectedListener(this);
        // 设置侧边头部视图点击事件
        View headView = nvMenu.getHeaderView(0);
        CircleImageView imgUserPic = headView.findViewById(R.id.img_userPic);
        tvUsername = headView.findViewById(R.id.tv_username);
        imgUserPic.setOnClickListener(this);
        // 初始化界面
        replaceFragment(new WordsFragment(this));
        // 判断是否登录
        if (BmobUser.isLogin()) {
            UserInfo userInfo = BmobUser.getCurrentUser(UserInfo.class);
            tvUsername.setText(userInfo.getUsername());
        }
    }

    /**
     * 替换碎片
     *
     * @param fragment 需要更换的碎片
     */
    private void replaceFragment(Fragment fragment) {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.fl_main_content, fragment);
        transaction.commit();
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        dyMenu.openDrawer(GravityCompat.START);
        return true;
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        toolbar.setTitle(menuItem.getTitle().toString());
        switch (menuItem.getTitle().toString()) {
            case "土味情话":
                replaceFragment(new WordsFragment(this));
                break;
            case "舔狗日记":
                replaceFragment(new CommunityFragment());
                break;
            case "设置·关于":
                replaceFragment(new SettingsFragment());
                break;
            default:
        }
        dyMenu.closeDrawers();
        return false;
    }

    @Override
    public void onClick(View v) {
        // 跳转至登录界面
        Intent intent = new Intent(this, LoginActivity.class);
        startActivityForResult(intent, MyApplication.LOGIN_REQUEST_CODE);
        dyMenu.closeDrawer(GravityCompat.START);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case MyApplication.LOGIN_REQUEST_CODE:
                if (data != null) {
                    // 显示用户名
                    String username = data.getStringExtra("username");
                    tvUsername.setText(username);

                }
                break;
            default:
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ToastUtil.cancelToast();
    }
}
